package com.idroc.coelacanth.core.message;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 消息编码器
 * @author yuyoo (yuyoo4j@163.com)
 * @date 2009-12-4 上午11:41:04
 */
public class MessageEncoder extends OneToOneEncoder {
	
	private static final Logger log = LoggerFactory.getLogger(MessageEncoder.class);

	@Override
	protected Object encode(ChannelHandlerContext ctx,
			Channel channel, Object obj) throws Exception {
		
		if (obj instanceof Message) {
			
			ChannelBuffer ob = ChannelBuffers.dynamicBuffer(channel.getConfig().getBufferFactory());
			Message msg = (Message) obj;
			AbstractMessage.encodeMessage(msg, ob);
			return ob;
		} else {
			log.debug("unencode obj:{}", obj);
		}
		return obj;
	}

}
